package com.game;

import java.util.Vector;


import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;


public class PlayScene extends Scene {
	
	
	
	private BackgroundMirror 	mBackgroundMirror;
	private BallManager 		mBallManager;
	private PowerUpManager 		mPowerUpManager;	
	
	private Laser 				mLaser;
	private Vector<MirrorBall> 	mMirrors;
	
	public Vector<Laser> 		extraLasers;	
	private int 				lastSelectedMirror;	
	public final int 			GameLife = 40;
	private Paint 				textpaint;
	private int 				currentLevel;
	

	public PlayScene(SceneManager parent)
	{
		super(parent);
		
		
		
		extraLasers = new Vector<Laser>();
		
		mBackgroundMirror = new BackgroundMirror(mContext, R.drawable.background, 
				R.drawable.backgroundc1, R.drawable.backgroundsc2, R.drawable.backgroundsc3,
				GameLife);
		
		mMirrors = new Vector<MirrorBall>();
		setBallManager(new BallManager(mContext, 2, mBackgroundMirror,mMirrors));	
		mPowerUpManager = new PowerUpManager(mContext, mBackgroundMirror, this);
		
		mLaser = new Laser(mContext,mMirrors, getBallManager(), mBackgroundMirror, R.drawable.beamhead3, R.drawable.beamedge, 
				R.drawable.beampoint, R.drawable.beamhead5, R.drawable.beamhead4, R.drawable.beamhead, R.drawable.beamhead2,
				mPowerUpManager.powerUps);
		
		// Create the 2 mirror balls
		MirrorBall auxMBall;
		auxMBall = new MirrorBall(mContext, R.drawable.ballmirror, (new Point()), mBackgroundMirror,0);
		mMirrors.add(auxMBall);
		auxMBall = new MirrorBall(mContext, R.drawable.ballmirror, (new Point()), mBackgroundMirror,1);
		mMirrors.add(auxMBall);
		lastSelectedMirror=0;
		
		textpaint= new Paint();
		textpaint.setARGB(200, 255, 0, 0);
		
		currentLevel = 0;
		
	}
	@Override
	public void Draw(Canvas canvas) {

		mBackgroundMirror.onDraw(canvas);
		String levelstring = Integer.toString(currentLevel);		
		canvas.drawText(levelstring, 20, 30, textpaint);
		
		String resHeight = Integer.toString(mParentSM.getThread().resolutionHeight);
		String resWidth = Integer.toString(mParentSM.getThread().resolutionWidth);	
		
		canvas.drawText(resWidth, 20, 60, textpaint);
		canvas.drawText(resHeight, 60, 60, textpaint);
		
		
		//mLaser.onDraw(canvas);
		getBallManager().onDraw(canvas);
		mPowerUpManager.Draw(canvas);
		
		for(int i = 0; i < mMirrors.size(); i++)
		{
			mMirrors.elementAt(i).onDraw(canvas);
		}
		for(Laser laser:extraLasers)
		{
			laser.onDraw(canvas);
		}
		mLaser.onDraw(canvas);

	}

	@Override
	public void End() {
		// TODO Auto-generated method stub

	}

	@Override
	public void Start() {

		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::Start" );
		
		currentLevel = 0;		
		GrowLevel();

	}

	@Override
	public void Update() {
		getBallManager().Update(); 
		mLaser.Update(); 
		mBackgroundMirror.Update(); 
		mPowerUpManager.Update();

		for(int i =0 ; i < extraLasers.size(); i++)
		{
			extraLasers.elementAt(i).Update(); 
			if(extraLasers.elementAt(i).ReadyToDie()) extraLasers.elementAt(i).inactive = true;
		}
		
		if(getBallManager().AllBallsDestroyed()) GrowLevel();
		else if (mBackgroundMirror.Destroyed()) GameOver();

	}
	
	
	@Override
	public boolean handleTouch(MotionEvent event)
	{
		synchronized (mSurfaceHolder) 
		{
			int msize = mMirrors.size();
			if(lastSelectedMirror < msize-1)
			{
				lastSelectedMirror++;
			}
			else
			{
				lastSelectedMirror = 0;
			}
			Point auxPoint = new Point();
			auxPoint.x = (int)event.getX();
			auxPoint.y = (int)event.getY();
			
			mMirrors.elementAt(lastSelectedMirror).moveMirror(auxPoint);
		}
		return true;
	}
	
	
	public void AddLaser()
	{
		Laser laser = new Laser(mContext, mMirrors, getBallManager(), mBackgroundMirror, R.drawable.beamhead3, R.drawable.beamedge, 
				R.drawable.beampoint, R.drawable.beamhead5, R.drawable.beamhead4, R.drawable.beamhead, R.drawable.beamhead2,
				mPowerUpManager.powerUps);
		laser.extraLaser = true;
		extraLasers.add(laser);
	}

	
	private void GrowLevel() 
	{
		Log.i("GAISMA LOG:: ",this.getClass().getName()+"::GrowLevel" );
		currentLevel++;		
		StartLevel();

	}
	
	public void StartLevel()
	{
		Log.i("GAISMA LOG:: ", " Level:  " + currentLevel);
		// Clean all balls
		getBallManager().CleanUp();	
		
		// Clean extra lasers
		extraLasers.clear();
	
		// Check random data
		int minSpeed, maxSpeed;
		minSpeed = getBallManager().getBaseBallSpeed() - (int)(currentLevel/4);
		if(minSpeed <= 0 ) minSpeed = 1;
		maxSpeed = getBallManager().getBaseBallSpeed() +(int) (currentLevel/2);
		if(maxSpeed> 10 ) maxSpeed = 10;
		
		int numbBalls = 1+ currentLevel/2;
		
		// Create level balls
		getBallManager().CreateBalls(numbBalls, minSpeed, maxSpeed);
		
		for (MirrorBall mball : mMirrors) 
		{
			mball.resetBallBool(numbBalls);
		}
		
		// Restore laser
		mLaser.Reset();
		
		// Restore mirror
		mBackgroundMirror.Reset(GameLife);
		
		// Restore PUPs
		mPowerUpManager.Reset();
	}

	public void GameOver()
	{
		currentLevel = 0;
		GrowLevel();
	}
	public void setBallManager(BallManager mBallManager) {
		this.mBallManager = mBallManager;
	}
	public BallManager getBallManager() {
		return mBallManager;
	}
	
	
	
}
